﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using Ro.AssistFunction.BackNodeInfo;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.RoWebAction;
using Ro.Common.RoInterface.UserType;

namespace Ro.DriverEvent.WebAction
{
    /// <summary>
    /// 接口实现
    /// 启动浏览器
    /// </summary>
    public class WebLaunch : IWebLaunchFunc
    {
        /// <summary>
        /// 启动firefix
        /// </summary>
        /// <param name="sigNode">Firefox节点</param>
        /// <returns>返回是否完成初始化</returns>
        public WebActionResult Firefox(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            FirefoxOptions options = new FirefoxOptions {BrowserExecutableLocation = RoPubArgments.BrowserDriverPath};

            //下载设置
            options.Profile.SetPreference("browser.download.dir", "C:/Users/%username%/Downloads/"); //指定下载路径
            options.Profile.SetPreference("browser.download.folderList", 2);
            options.Profile.SetPreference("browser.download.manager.showWhenStarting", false);
            options.Profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "images/jpeg, application/pdf, application/octet-stream"); //指定下载文件格式


            try
            {
                //启动firefox浏览器
                RoTestObject.WebTestObject = new FirefoxDriver(options);
                RoTestObject.WebTestObject.Manage().Timeouts().PageLoad = timeout;

                //获取url值
                string url = extractNeedInfo.ExtractAttributeValue(sigNode, "web:Url");
                ArgsIntoValue argsIntoValue= new ArgsIntoValue(url);
                url = argsIntoValue.BackValue;
                RoTestObject.WebTestObject.Navigate().GoToUrl(url);
                //设置长和高
                RoTestObject.WebTestObject.Manage().Window.Maximize();
                //RoTestObject.WebTestObject.Manage().Window.Size = new Size(1366, 768);

                back = BackActionResult.BackWebResult(true, "未使用", "没有附加信息"); //结果
            }

            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, "未使用", "启动FireFox操作遭遇系统异常"); //结果
                ExceptionOutPut.SystemExceptionOut("FireFox", e);
            }

            return back;
        }

        /// <summary>
        /// 启动Chrome
        /// </summary>
        /// <param name="sigNode">Chrome</param>
        /// <returns>返回是否完成初始化</returns>
        public WebActionResult Chrome(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //下载路径
            ChromeOptionsWithPrefs options = new ChromeOptionsWithPrefs
            {
                Prefs = new Dictionary<string, object>
                {
                    {"download.default_directory", @"C:/Users/%username%/Downloads/"},
                    {"download.prompt_for_download", false}
                }
            };

            try
            {
                //启动chrome浏览器
                RoTestObject.WebTestObject = new ChromeDriver(RoPubArgments.BrowserDriverPath, options);
                RoTestObject.WebTestObject.Manage().Timeouts().PageLoad = timeout;

                //获取url值
                string url = extractNeedInfo.ExtractAttributeValue(sigNode, "web:Url");
                ArgsIntoValue argsIntoValue = new ArgsIntoValue(url);
                url = argsIntoValue.BackValue;
                RoTestObject.WebTestObject.Navigate().GoToUrl(url);

                //设置长和高
                RoTestObject.WebTestObject.Manage().Window.Maximize();
                //RoTestObject.WebTestObject.Manage().Window.Size = new Size(1366, 768);
                back = BackActionResult.BackWebResult(true, "未使用", "没有附加信息"); //结果
            }

            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, "未使用", "启动Chrome操作遭遇系统异常"); //结果
                ExceptionOutPut.SystemExceptionOut("Chrome", e);
            }

            return back;
        }

        /// <summary>
        /// 启动explorer
        /// </summary>
        /// <param name="sigNode">Explorer</param>
        /// <returns>返回是否完成初始化</returns>
        public WebActionResult Explorer(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            InternetExplorerOptions options = new InternetExplorerOptions();

            try
            {
                RoTestObject.WebTestObject = new InternetExplorerDriver(RoPubArgments.BrowserDriverPath, options);
                RoTestObject.WebTestObject.Manage().Timeouts().PageLoad = timeout;
                //获取url值
                string url = extractNeedInfo.ExtractAttributeValue(sigNode, "web:Url");
                ArgsIntoValue argsIntoValue = new ArgsIntoValue(url);
                url = argsIntoValue.BackValue;
                RoTestObject.WebTestObject.Navigate().GoToUrl(url);
                //设置长和高
                RoTestObject.WebTestObject.Manage().Window.Maximize();
                //RoTestObject.WebTestObject.Manage().Window.Size = new Size(1366, 768);

                back = BackActionResult.BackWebResult(true, "未使用", "没有附加信息"); //结果
            }

            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, "未使用", "启动Explorer操作遭遇系统异常"); //结果
                ExceptionOutPut.SystemExceptionOut("Explorer", e);
            }

            return back;
        }


        /// <summary>
        /// 启动Chrome Option 的Prefs
        /// </summary>
        private class ChromeOptionsWithPrefs : ChromeOptions
        {
            public Dictionary<string, object> Prefs { get; set; }
        }
    }
}